<?php
/**
 * Spellbook: Web application framework
 *
 * @author       Jorge Pena <jmgpena@gmail.com>
 * @version 3.0
 * @package sbook
 */

// Path to the application's directory.
define ('APPS', ROOT.'apps'.DS);

// Path to the configuration files directory.
define ('CONFIG', ROOT.'config'.DS);

// Path to the libs directory.
define ('LIBS', ROOT.'libs'.DS);

// Path to the external libs directory.
define ('EXTERNAL', LIBS.'external'.DS);

// Path to the logs directory.
define ('LOGS', ROOT.'logs'.DS);

// The hostname part of the URI 
define('SERVER_NAME',$_SERVER['HTTP_HOST']);

// Full url prefix
define('FULL_BASE_URL', 'http://'.SERVER_NAME.'/');

// Base path for urls
$baseuri = config::file()->get('baseuri');
if (isset($baseuri))
    define(BASEURI,$baseuri);
else
    define(BASEURI,FULL_BASE_URL);

/*
 * This uses the PHP5 class autoload to load classes for the app or the framework
 * If there are name clashes confusion might ensue so be careful...
 */
function __autoload($class_name)
{
    // First looks on the app libs directory
    if (defined(APPLIBS) && file_exists(APPLIBS.$class_name.'.php'))
    {
        require_once(APPLIBS.$class_name.'.php');
    }
    // Then looks in the main libs directory
    elseif (file_exists(LIBS.$class_name.'.php'))
    {
        require_once(LIBS.$class_name.'.php');
    }
}

/*
 * User defined exception handler.
 */
function sbook_exception_handler($exception)
{
    echo "sbook::error<br>" , $exception->getMessage(), "\n";
}
set_exception_handler('sbook_exception_handler');

/*
 * Load libraries manually
 */
function sb_loadLibs()
{
    $args = func_get_args();
    foreach ($args as $arg)
    {
        $arg = strtolower($arg);

        // First looks on the app libs directory
        if (defined(APPLIBS) && file_exists(APPLIBS.$arg.'.php'))
        {
            require_once(APPLIBS.$arg.'.php');
        }
        // Then looks in the main libs directory
        elseif (file_exists(LIBS.$arg.'.php'))
        {
            require_once(LIBS.$arg.'.php');
        }
    }
}
?>
